#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

/* 
 */

using namespace std;

#define NUM 5

void * threadRun(void * name)
{
	while (true) {
		cout << "I am other" << (char*)name << endl;
		sleep(1);
	}
	delete[] (char *)name;

	return NULL;
}

int main()
{
	pthread_t pt[NUM];

	for (int i = 0; i < NUM; i++) {
		char *name = new char[64];
		snprintf(name, 64, "thread-%d", i+1);
		pthread_create(pt+i, NULL, threadRun, name);
	}

	for (int i = 0; i < NUM; i++) {
		int ret = pthread_join(pt[i], NULL);
		if (ret != 0)
			cout << "wait thread" << pt[i] << "fail" << endl;
	}

	/* 
	 * if have pthread_join, 先等pt[i] thread 结束，因pt[i]线程死循环，所以程序一直运行。
	 * if have not pthread_join, 只输出 all thread exited，程序整个退出。
	 */
	cout << "all thread exited" << endl;

	return 0;
}
